package com.zcp.recycleviewmoreitem;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2017/3/5.
 */

public class RecycleAda extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    LayoutInflater inflater;
    List<Bean.ResultBean.DataBean> list;
    int TYPE_ITEM1 = 0;
    int TYPE_ITEM2 = 1;
    Call2 cc;
    Context context;

    public interface Call2 {
        void setUrl(String url);
    }

    public void setListener(Call2 call) {
        this.cc = call;
    }

    public RecycleAda(Context context, List<Bean.ResultBean.DataBean> list) {
        this.inflater = LayoutInflater.from(context);
        this.list = list;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final RecyclerView.ViewHolder vh;
        if (TYPE_ITEM1 == viewType) {
            View view = inflater.inflate(R.layout.v_item1, parent, false);
            vh = new MyViewHolder(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "哈哈", Toast.LENGTH_SHORT).show();
                    int position = vh.getAdapterPosition();
                    cc.setUrl(list.get(position).url);
                }
            });
        } else {
            View view = inflater.inflate(R.layout.lv_item2, parent, false);
            vh = new MyViewHolder2(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "呵呵", Toast.LENGTH_SHORT).show();
                    int position = vh.getAdapterPosition();
                    cc.setUrl(list.get(position).url);

                }
            });
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            ImageLoader.getInstance().displayImage(list.get(position).thumbnail_pic_s, ((MyViewHolder) holder).image, MainActivity.options);
            ((MyViewHolder) holder).tv_title.setText(list.get(position).title);
            //
        } else if (holder instanceof MyViewHolder2) {
            ImageLoader.getInstance().displayImage(list.get(position).thumbnail_pic_s, ((MyViewHolder2) holder).image1, MainActivity.options);
            ImageLoader.getInstance().displayImage(list.get(position).thumbnail_pic_s02, ((MyViewHolder2) holder).image2, MainActivity.options);
            ImageLoader.getInstance().displayImage(list.get(position).thumbnail_pic_s03, ((MyViewHolder2) holder).image3, MainActivity.options);
            ((MyViewHolder2) holder).tv_title2.setText(list.get(position).title);
        }

    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return TYPE_ITEM2;
        } else {
            return TYPE_ITEM1;
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private final ImageView image;
        private final TextView tv_title;

        public MyViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);

        }
    }

    class MyViewHolder2 extends RecyclerView.ViewHolder {

        private final ImageView image1;
        private final ImageView image2;
        private final ImageView image3;
        private final TextView tv_title2;

        public MyViewHolder2(View itemView) {
            super(itemView);
            image1 = (ImageView) itemView.findViewById(R.id.image1);
            image2 = (ImageView) itemView.findViewById(R.id.image2);
            image3 = (ImageView) itemView.findViewById(R.id.image3);
            tv_title2 = (TextView) itemView.findViewById(R.id.tv_title2);

        }
    }
}
